﻿#region Copyright(c) 1998-2014, Arnaud Colin Microsoft Public License (Ms-PL)
/* Copyright(c) 1998-2014, Arnaud Colin
 * All rights reserved.
 */
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;





namespace T125Protocol
{

    public class ConnectionT125 : Connection
    {

         public ConnectionT125(string Address, short port) : base(Address, port)
         {
         }
        /*
         public ReceiveData Connect( string pid, Trame  Appel, Trame action)
         {
             Socket ss = null;
             byte[] RcpBuf = new byte[2048];

             ReceiveData Result = null;

             if( (ss = Open())  == null)
                 return Result;

             int iRecu = 0;
             byte[] buf = null;
             ushort index = 0;
             qTermForm.GetTIPInit(pid).Write(ref buf, ref index);

             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 return Result;

             if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 return Result;

             Trame tRecu = new Trame(RcpBuf);

             if (tRecu.PDU == PDU_TYPE.CONNECT_CONFIRMATION)
             {
                 if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                     return Result;
                 tRecu = new Trame(RcpBuf);

                 if (ss.Available > 0)
                 {
                     iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None);
                     tRecu = new Trame(RcpBuf);
                 }
             }
             else
                 return Result;

             index = 0;
             buf = null;
             Appel.Write(ref buf, ref index);

             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 return Result;

             if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 return Result;
             tRecu = ReceiveField<Trame>(ref RcpBuf);

             if (ss.Available > 0)
             {
                 if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                     return Result;

                 tRecu = ReceiveField<Trame>(ref RcpBuf);
             }

             if (tRecu.Data.Count == 2 && iRecu < 200)
             {
                 index = 0;
                 qTermForm.GetClean(tRecu.Data._Entete).Write(ref buf, ref index);

                 if (!Send(ref ss, ref  buf, SocketFlags.None))
                     return null;

                 index = 0;
                 buf = null;
                 Appel.Write(ref buf, ref index);

                 if (!Send(ref ss, ref  buf, SocketFlags.None))
                     return Result;

                 if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                     return Result;
                 tRecu = ReceiveField<Trame>(ref RcpBuf);
             }

             index = 0;
             buf = null;
             action.Write(ref buf, ref index);

             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 return Result;

             if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 return Result;
             tRecu = ReceiveField<Trame>(ref RcpBuf);


             Result = tRecu.Data;

             if (tRecu.Data.Count == 2)
             {
                 CLogOnFile.WriteMessage(tRecu.Data.chant[0].Text).Where().Write();
             }

             ss.Disconnect(true);
             ss = null;

             return Result;
         }
        
         public ReceiveData Connect( string pid ,string Article, short depot, short ste, string client, char key)
        {

            Socket ss = null;
            byte[] RcpBuf = new byte[2048];

            ReceiveData Result = null;

            if ((ss = Open()) == null)
                return Result;

            int iRecu = 0;
            byte[] buf = null;
            ushort index = 0;
            qTermForm.GetTIPInit(pid).Write(ref buf, ref index);

             if(!Send(ref ss, ref  buf, SocketFlags.None) )
                 return Result;

             if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 return Result;
                   
            Trame tRecu = new Trame(RcpBuf);

#if FILETRACE
            tRecu.Fichier("c:\\File1.txt");
#endif

            if (tRecu.PDU == PDU_TYPE.CONNECT_CONFIRMATION)
            {


                    if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.Partial)) == 0)
                        return Result;
                    tRecu = new Trame(RcpBuf);

#if FILETRACE
                tRecu.Fichier("c:\\File2.txt");
#endif

                if (ss.Available >0)
                {
                    iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None);
                    tRecu = new Trame(RcpBuf);
#if FILETRACE
                    tRecu.Fichier("c:\\File3.txt");
#endif
                }
            }
            else
                return Result;

            index = 0;
            qTermForm.GetAppel("TRANSMANE ").Write(ref buf, ref index);
            
            if (!Send(ref ss, ref  buf, SocketFlags.None))
                return Result;

            if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                return Result;
            tRecu = ReceiveField<Trame>(ref RcpBuf);
#if FILETRACE
            tRecu.Fichier("c:\\File4.txt");
#endif

            if (ss.Available > 0)
            {
                if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                    return Result;

                tRecu = ReceiveField<Trame>(ref RcpBuf);
#if FILETRACE
                tRecu.Fichier("c:\\File5.txt");
#endif
            }

            if (tRecu.Data.Count == 2 && iRecu < 200)
            {

                index = 0;
                qTermForm.GetClean(tRecu.Data._Entete).Write(ref buf, ref index);
                if(!Send(ref ss, ref  buf, SocketFlags.None) )
                   return null;


                index = 0;
                qTermForm.GetAppel("TRANSNAME ").Write(ref buf, ref index);
                if (!Send(ref ss, ref  buf, SocketFlags.None))
                    return Result;

                if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                    return Result;
                tRecu = ReceiveField<Trame>(ref RcpBuf);
#if FILETRACE
                tRecu.Fichier("c:\\File6.txt");
#endif
            }

            //Thread.Sleep(50);
            index = 0;
            INSTOCK.GetAction(Article, depot, ste, client, key).Write(ref buf, ref index);
            if (!Send(ref ss, ref  buf, SocketFlags.None))
                return Result;
          
            if ((iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                return Result;
            tRecu = ReceiveField<Trame>(ref RcpBuf);
#if FILETRACE
            tRecu.Fichier("c:\\File7.txt");
#endif

            Result = tRecu.Data;

            if (tRecu.Data.Count == 2)
             {
                 CLogOnFile.WriteMessage(tRecu.Data.chant[0].Text).Where().Write();
             }


            //iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None);
            //tRecu = ReceiveField(ref RcpBuf);

Exit:
            ss.Disconnect(true);
            ss = null;

            return Result;
        }

         * */
/*
        Trame ReceiveField(ref byte[] buf)
        {
            Trame tRecu = new Trame(null, new ReceiveData());
            ushort Index = 0;
            tRecu.Read(buf, ref Index);

            return tRecu;
        }*/


         public void rr(string truc)
         {
             StreamWriter _fs = null;

             if (_fs == null)
             {
                 _fs = new StreamWriter(@"c:\Trame.txt", true);
             }
             
             _fs.WriteLine(truc);

             _fs.Flush();

             _fs.Close();
             _fs = null;
         }


         public ReceiveData Connect3(string pid, string Article, short depot, short ste, string client, char key)
         {

             Socket ss = null;
             byte[] RcpBuf = new byte[2048];
             ReceiveData Result = null;

             if ((ss = Open()) == null)
                 return Result;

             ss.ReceiveTimeout = 3000;

             int iRecu = 0;
             byte[] buf = null;
             ushort index = 0;
             qTermForm.GetTIPInit(pid).Write(ref buf, ref index);

             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 return Result;

             while (true)
             {
                 try
                 {
                     iRecu = ss.ReceiveFrom(RcpBuf, ref _Server);
                 }
                 catch (SocketException)
                 {
                     rr("Time Out");
                     goto Exit;
                 }
                 if (iRecu != 0)
                 {
                     var tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu);
                     if (SendResponse(tRecu, ref Result) == null)
                         goto Exit;

                     rr(ByteToString.GetString(RcpBuf, iRecu));
                 }
             }

         Exit:
             ss.Disconnect(true);
             ss = null;

             return Result;

         }

         private object SendResponse(List<Trame> t, ref ReceiveData Result)
         {
             throw new NotImplementedException();
         }

         //public delegate TResult Func<in T, out TResult>(T arg);


         public ReceiveData Connect2(string pid, string Article, short depot, short ste, string client, char key)
         {

#if DEBUG
             bool erased = false;
#endif

             Socket ss = null;
             byte[] RcpBuf = new byte[2048];
             ReceiveData Result = null;

             if ((ss = Open()) == null)
                 return Result;

             ss.ReceiveTimeout = 3000;

             List<Trame> tRecu;
             int iRecu = 0;
             byte[] buf = null;
             ushort index = 0;
             qTermForm.GetTIPInit(pid).Write(ref buf, ref index);

             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 return Result;

             if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 goto Exit;

             if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu )) == null)
                 goto Exit;
#if DEBUG
             foreach (Trame xx in tRecu)
                 xx.Fichier("c:\\File1.txt", erased);
#endif
             var gg = tRecu.Where(p => p.PDU == PDU_TYPE.CONNECT_CONFIRMATION).Count();

             if (tRecu.First().PDU == PDU_TYPE.CONNECT_CONFIRMATION)
             {

                 if (tRecu.Count == 1)
                 {
                 /* if (ss.Available > 0)
                  {*/
                 if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.Partial)) == 0)
                     goto Exit;

                 if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                     goto Exit;

#if DEBUG
                 foreach (Trame xx in tRecu)
                     xx.Fichier("c:\\File2.txt", erased);
#endif

                // tRecu = new Trame(RcpBuf);
                  }

                 if (ss.Available > 0)
                 {
                     iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None);
                     if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                         goto Exit;
#if DEBUG
                     foreach (Trame xx in tRecu)
                         xx.Fichier("c:\\File3.txt", erased);
#endif

                 }
             }
             else
                 goto Exit;

             index = 0;
             qTermForm.GetAppel("TRANSNAME " /* Name of the transaction */).Write(ref buf, ref index);

             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 goto Exit;

             if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 goto Exit;
             if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                 goto Exit;
#if DEBUG
             foreach (Trame xx in tRecu)
                 xx.Fichier("c:\\File4.txt", erased);
#endif

             if (ss.Available > 0)
             {
                 if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                     goto Exit;

                 if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                     goto Exit;
#if DEBUG
                 foreach (Trame xx in tRecu)
                     xx.Fichier("c:\\File5.txt", erased);
#endif
             }

             var gssg = tRecu.First();

             if (gssg.Data.Count == 2 && iRecu < 200)
             {

                 index = 0;
                 qTermForm.GetClean(gssg.Data._Entete).Write(ref buf, ref index);
                 if (!Send(ref ss, ref  buf, SocketFlags.None))
                     goto Exit;


                 index = 0;
                 qTermForm.GetAppel("TRANSNAME " /* Name of the transaction */).Write(ref buf, ref index);
                 if (!Send(ref ss, ref  buf, SocketFlags.None))
                     goto Exit;

                 if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                     goto Exit;
                 if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                     goto Exit;
#if DEBUG
                 foreach (Trame xx in tRecu)
                     xx.Fichier("c:\\File6.txt", erased);
#endif
             }

             //Thread.Sleep(50);
             index = 0;
             INSTOCK.GetAction(Article, depot, ste, client, key).Write(ref buf, ref index);
             if (!Send(ref ss, ref  buf, SocketFlags.None))
                 goto Exit;

             if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                 goto Exit;

             if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                 goto Exit;
#if DEBUG
             foreach (Trame xx in tRecu)
                 xx.Fichier("c:\\File7.txt", erased);
#endif
         
             

             Result = tRecu.First().Data;

             if (Result.Count == 2)
             {
               //  CatchMe.WriteMessage(Result.chant[0].Text).Where().Write();
             }

             if (ss.Available > 0)
             {
                 if ((iRecu = ReceiveFrom(ref ss, ref RcpBuf, SocketFlags.None)) == 0)
                     goto Exit;

                 if ((tRecu = ReceiveField<Trame>(ref RcpBuf, iRecu)) == null)
                     goto Exit;
#if DEBUG
                 foreach (Trame xx in tRecu)
                     xx.Fichier("c:\\File8.txt", erased);
#endif
             }


                  //iRecu = Receive(ref ss, ref RcpBuf, SocketFlags.None);
         //tRecu = ReceiveField(ref RcpBuf);

      Exit:
             ss.Disconnect(true);
             ss = null;

             return Result;
         }


    }
}
